/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.sonrisas.tools.view;

import Complementos.Conexion_BD;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author DIEGOALV
 */
public class frmAsignarPerfil extends javax.swing.JFrame {

    /**
     * Creates new form frmAsignarPerfil
     */
    public frmAsignarPerfil() {
        initComponents();
        cargarComponentes();
        cargarTabla();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tblReporte = new javax.swing.JTable();
        jPanel3 = new javax.swing.JPanel();
        comboPerfil = new javax.swing.JComboBox();
        jScrollPane2 = new javax.swing.JScrollPane();
        txtDescripcion = new javax.swing.JTextArea();
        btnBuscar = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        btnVisualizar = new javax.swing.JButton();
        txtId = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(204, 204, 255));
        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Asignar Perfiles", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tunga", 1, 18), new java.awt.Color(255, 255, 153))); // NOI18N

        tblReporte.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null},
                {null, null, null, null, null, null, null}
            },
            new String [] {
                "Login", "Nombre", "Apellido Paterno", "Apellido Materno", "DNI", "Perfil", "IdPerfil"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, false, false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tblReporte);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 823, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addGap(0, 11, Short.MAX_VALUE)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 155, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        jPanel3.setBackground(new java.awt.Color(204, 204, 255));
        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Perfiles", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Tunga", 1, 18), new java.awt.Color(255, 255, 153))); // NOI18N

        comboPerfil.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                comboPerfilItemStateChanged(evt);
            }
        });

        txtDescripcion.setColumns(7);
        txtDescripcion.setEditable(false);
        txtDescripcion.setRows(5);
        jScrollPane2.setViewportView(txtDescripcion);

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/sonrisas/tools/imagenes/comun/adduser1.png"))); // NOI18N
        btnBuscar.setText("ASIGNAR");
        btnBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBuscarActionPerformed(evt);
            }
        });

        jLabel2.setText("Descripcion:");

        jLabel1.setText("Buscar:");

        btnVisualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pe/sonrisas/tools/imagenes/comun/horario.png"))); // NOI18N
        btnVisualizar.setText("VER PERMISOS");
        btnVisualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnVisualizarActionPerformed(evt);
            }
        });

        txtId.setEditable(false);

        jLabel3.setText("ID");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel3Layout.createSequentialGroup()
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(20, 20, 20)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(comboPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(jLabel3)
                        .addGap(18, 18, 18)
                        .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jScrollPane2)
                            .addGroup(jPanel3Layout.createSequentialGroup()
                                .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 105, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGap(18, 18, 18)
                                .addComponent(btnVisualizar)
                                .addGap(0, 0, Short.MAX_VALUE)))))
                .addGap(238, 238, 238))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {btnBuscar, btnVisualizar});

        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(17, 17, 17)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(comboPerfil, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(jLabel3)
                    .addComponent(txtId, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(47, 47, 47)
                        .addComponent(jLabel2)))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnVisualizar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(20, Short.MAX_VALUE))
        );

        jPanel3Layout.linkSize(javax.swing.SwingConstants.VERTICAL, new java.awt.Component[] {btnBuscar, btnVisualizar});

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(195, 195, 195)
                        .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(35, 35, 35)
                .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(28, 28, 28)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(31, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBuscarActionPerformed
        try {
            Conexion_BD co=new Conexion_BD();
            co.Conectar();
            
            int fila = tblReporte.getSelectedRow();
            
            String query = "UPDATE usuario SET idPerfil = "+txtId.getText()+" WHERE login = '"+tblReporte.getValueAt(fila, 0)+"'";
            co.insupdel(query);
            co.Desconectar();
            limpiarTabla();
            cargarTabla();
            
            JOptionPane.showMessageDialog(this, "Se actualizó el perfil correctamente", "Perfil actualizado", JOptionPane.INFORMATION_MESSAGE);
            
            
        } catch (Exception ex) {
            Logger.getLogger(frmAsignarPerfil.class.getName()).log(Level.SEVERE, null, ex);
        } 
        
        
        
        
        

    }//GEN-LAST:event_btnBuscarActionPerformed

    
    private void comboPerfilItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_comboPerfilItemStateChanged
        
      try {
            Conexion_BD conexion = new Conexion_BD();

            conexion.Conectar();
            
            if (comboPerfil.getSelectedItem().toString()=="Seleccione"){
                
                txtDescripcion.setText("");
                
            }
            
            String opcion = comboPerfil.getSelectedItem().toString();
            ResultSet datos = null;

            String query = "select id,descripcion from perfil where nombre ='" + opcion + "'";
            datos = conexion.Consultar(query);

            while (datos.next()) {
                txtId.setText(datos.getString(1));
                txtDescripcion.setText(datos.getString(2));

            }
            try {
                conexion.Desconectar();
            } catch (Exception ex) {
                Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }  
        
    }//GEN-LAST:event_comboPerfilItemStateChanged

    private void btnVisualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnVisualizarActionPerformed

        if (comboPerfil.getSelectedIndex() != 0) {
            frmVisualizarPermisos objeto = new frmVisualizarPermisos();

            objeto.setVisible(true);
            objeto.setCodigo(txtId.getText());
        } else {
            JOptionPane.showMessageDialog(this, "Seleccione un perfil");
        }


    }//GEN-LAST:event_btnVisualizarActionPerformed

    public void limpiarTabla(){
        int fila = 0;
            while (fila < tblReporte.getRowCount()){
            
            tblReporte.setValueAt("", fila, 0);
            tblReporte.setValueAt("", fila, 1);
            tblReporte.setValueAt("", fila, 2);
            tblReporte.setValueAt("", fila, 3);
            tblReporte.setValueAt("", fila, 4);
            tblReporte.setValueAt("", fila, 5);
            tblReporte.setValueAt("", fila, 6);
            fila++;
            
            }
    
    }
  
    
    public static void main(String args[]) {
      java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmAsignarPerfil().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnVisualizar;
    private javax.swing.JComboBox comboPerfil;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTable tblReporte;
    private javax.swing.JTextArea txtDescripcion;
    private javax.swing.JTextField txtId;
    // End of variables declaration//GEN-END:variables

    private void cargarComponentes() {
         try {
            Conexion_BD co = new Conexion_BD();
                ResultSet rs = null;
                
                co.Conectar();
                
                String query = "select id,nombre from perfil";
                System.out.println(query);
                rs = co.Consultar(query);
            
                comboPerfil.addItem("Seleccione");
                
                while(rs.next()){
                    comboPerfil.addItem(rs.getString(2));
                }
            try {
                co.Desconectar();
            } catch (Exception ex) {
                Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(frmMantenerPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    public void cargarTabla() {
        try {
            Conexion_BD co = new Conexion_BD();
            ResultSet rs = null;
            co.Conectar();
            
            String q = "select u.login,p.nombres,p.apellidoPaterno,p.apellidoMaterno,p.dni,per.nombre,per.id from usuario u ,persona p ,perfil per WHERE u.idPERSONA = p.idPERSONA AND  u.idPerfil = per.id"; 
           
            System.out.println(q);
            rs = co.Consultar(q);
            int fila = 0;
            while (rs.next()){
            
            tblReporte.setValueAt(rs.getString(1), fila, 0);
            tblReporte.setValueAt(rs.getString(2), fila, 1);
            tblReporte.setValueAt(rs.getString(3), fila, 2);
            tblReporte.setValueAt(rs.getString(4), fila, 3);
            tblReporte.setValueAt(rs.getString(5), fila, 4);
            tblReporte.setValueAt(rs.getString(6), fila, 5);
            tblReporte.setValueAt(rs.getString(7), fila, 6);
            fila++;
            
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(frmAsignarPerfil.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(frmAsignarPerfil.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    }   

